package service

import (
	"context"
	"errors"
	"gitee.com/Frank098/container-management/model"
	"gitee.com/Frank098/container-management/utils"
	"github.com/wonderivan/logger"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"strconv"
	"strings"
	"time"
)

var Node node

type node struct{}

// 获取node详情
func (n *node) GetNodeInfo(nodeName string) (node *corev1.Node, err error) {
	node, err = K8s.ClientSet.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
	if err != nil {
		logger.Error(errors.New("获取Node详情失败, " + err.Error()))
		return nil, errors.New("获取Node详情失败, " + err.Error())
	}

	return node, nil
}

func (n *node) GetNodeAll() (nodesList []*model.GetNodesList, err error) {
	nodeList, err := K8s.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		return nil, errors.New("获取Node列表失败, " + err.Error())
	}
	for _, nodeListInfo := range nodeList.Items {
		nodeMetricses, err := K8s.Mc.MetricsV1beta1().NodeMetricses().Get(context.TODO(), nodeListInfo.Name, metav1.GetOptions{})
		if err != nil {
			return nil, errors.New("获取Node指标失败," + err.Error())
		}

		usageCpu := nodeMetricses.Usage.Cpu().String()
		usageCpuStr, err := n.GetNodesCpu(usageCpu)
		if err != nil {
			return nil, err
		}

		usageMem := nodeMetricses.Usage.Memory().String()
		usageMemStr, err := n.GetNodesMemory(usageMem)
		if err != nil {
			return nil, err
		}

		capacityMem := nodeListInfo.Status.Capacity.Memory().String()
		capacityMemStr, err := n.GetNodesMemory(capacityMem)
		if err != nil {
			return nil, err
		}

		currentContainerTime := nodeListInfo.CreationTimestamp
		dateOfDifference := utils.SubDays(time.Now(), currentContainerTime.Time)
		nodesListInfos := &model.GetNodesList{
			Status:  string(nodeListInfo.Status.Conditions[len(nodeListInfo.Status.Conditions)-1].Type),
			Name:    nodeListInfo.Name,
			Version: nodeListInfo.Status.NodeInfo.KubeletVersion,
			Address: nodeListInfo.Status.Addresses[0].Address,
			Arch:    nodeListInfo.Status.NodeInfo.Architecture,
			Cpu:     nodeListInfo.Status.Capacity.Cpu().String() + " / " + usageCpuStr,
			Memory:  capacityMemStr + "Mi" + " / " + usageMemStr + "Mi",
			Age:     dateOfDifference,
		}
		//添加到podsNps数组中
		nodesList = append(nodesList, nodesListInfos)
	}
	return nodesList, nil
}

func (n *node) GetNodesCpu(cpuStr string) (meanCpuStr string, err error) {
	cpuArr := strings.Split(cpuStr, "n")
	getCpuArr := cpuArr[0]
	getCpuInt, err := strconv.Atoi(getCpuArr)
	if err != nil {
		return "", errors.New("cpu数据类型转换失败" + err.Error())
	}
	meanCpu := float64(getCpuInt) / 100000000
	meanCpuStr = strconv.FormatFloat(meanCpu, 'g', 2, 32)
	return meanCpuStr, nil
}

func (n *node) GetNodesMemory(memStr string) (getMemStr string, err error) {
	memArr := strings.Split(memStr, "Ki")
	getMemArr := memArr[0]
	getMemInt, err := strconv.Atoi(getMemArr)
	if err != nil {
		return "", errors.New("内存数据类型转换失败" + err.Error())
	}
	getMem := getMemInt / 1024
	getMemStr = strconv.Itoa(getMem)
	return getMemStr, nil
}
